package com.example.administrator.coldlain.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.Spinner;
import android.widget.Toast;
import android.widget.ToggleButton;

import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;
import com.example.administrator.coldlain.Api.ApiService;
import com.example.administrator.coldlain.R;
import com.example.administrator.coldlain.db.VehicleDBHelper;
import com.example.administrator.coldlain.model.InsertUser;
import com.google.gson.Gson;

import org.json.JSONException;
import org.json.JSONObject;

public class RegistActivity extends Activity implements View.OnClickListener {


    private EditText username;
    private EditText password;
    private RadioButton male;
    private RadioButton female;
    private ToggleButton marraged;
    private CheckBox reading;
    private CheckBox swimming;
    private CheckBox running;
    private Button cancelbtn;
    private Button registbtn;
    private Spinner position;
   // DBHelper dbHelper;
    VehicleDBHelper vehicleDBHelper;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_regist);
        initView();
        //dbHelper=new DBHelper(this);
        vehicleDBHelper=new VehicleDBHelper(this);
    }

    private void initView() {
        username = (EditText) findViewById(R.id.username);
        password = (EditText) findViewById(R.id.password);
        male = (RadioButton) findViewById(R.id.male);
        female = (RadioButton) findViewById(R.id.female);
        marraged = (ToggleButton) findViewById(R.id.marraged);
        reading = (CheckBox) findViewById(R.id.reading);
        swimming = (CheckBox) findViewById(R.id.swimming);
        running = (CheckBox) findViewById(R.id.running);
        cancelbtn = (Button) findViewById(R.id.cancelbtn);
        registbtn = (Button) findViewById(R.id.registbtn);
        position = (Spinner) findViewById(R.id.position);
        String[] array={"CEO","CFO","PM"};
        ArrayAdapter arrayAdapter=new ArrayAdapter(this,android.R.layout.simple_spinner_item,array);
        position.setAdapter(arrayAdapter);
        cancelbtn.setOnClickListener(this);
        registbtn.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.cancelbtn:
                break;
            case R.id.registbtn:
                submit();
                Bundle b = new Bundle();
                b.putString("username", "用户名:"+username.getText().toString());
                b.putString("password", "密码:"+password.getText().toString());
                String ssex = "";
                if (male.isChecked())
                    ssex = "男";
                else
                    ssex = "女";
                b.putString("ssex", ssex);
                String hobby = "爱好：";
                if (reading.isChecked())
                    hobby = hobby + "阅读";
                if (swimming.isChecked())
                    hobby = hobby + "阅读";
                if (running.isChecked())
                    hobby = hobby + "跑步";
                b.putString("hobby", hobby);
                String marriaged = "婚否：";
                if (marraged.isChecked())
                    marriaged = marriaged + "已婚";
                else
                    marriaged = marriaged + "未婚";
                b.putString("marriaged", marriaged);
                b.putString("position", "职务："+position.getSelectedItem().toString());

//                "CREATE TABLE `usertbl` (" +
//                        "id integer primary key autoincrement ," +
//                        "account text," +
//                        "password text," +
//                        "name text," +
//                        "gender, text," +
//                        "favorite text," +
//                        "remark text)";
//                ContentValues contentValues=new ContentValues();
//                contentValues.put("username",username.getText().toString());
//                contentValues.put("password",password.getText().toString());
//                contentValues.put("ssex",ssex);
//                contentValues.put("favorite",hobby);
//                contentValues.put("marrage",marriaged);
//                contentValues.put("position",position.getSelectedItem().toString());
//                int i=new VehicleDBHelper(this).insertuser(contentValues);
//                if(i>0) {
//                    Toast.makeText(this, "用户注册成功", Toast.LENGTH_SHORT).show();
//                    startActivity(new Intent(RegistActivity.this,ResultActivity.class));
//                }
//                else
//                    Toast.makeText(this, "用户注册失败", Toast.LENGTH_SHORT).show();
//                Intent intent = new Intent(RegistActivity.this, ResultActivity.class);
//                intent.putExtra("data", b);
//                startActivity(intent);
                //String url="10.70.64.163:8080/MyTest/InsertUser?account='"+username.getText().toString()+"'&password=123&name=Test&gender=女&favorite=羽毛球 骑行";

                JSONObject jsonObject=new JSONObject();
                try {
                    jsonObject.put("account",username.getText().toString());
                    jsonObject.put("password",password.getText().toString());
                    jsonObject.put("name","张三");
                    jsonObject.put("gender",ssex);
                    jsonObject.put("favorite",hobby);
                } catch (JSONException e) {
                    e.printStackTrace();
                }


                JsonObjectRequest jsonObjectRequest=new JsonObjectRequest(
                        Request.Method.POST,
                        "http://192.168.137.1:8080/MyTest/InsertUserServlet",
                        jsonObject,
                        new Response.Listener<JSONObject>() {
                            @Override
                            public void onResponse(JSONObject jsonObject) {
                                Gson gson=new Gson();
                                InsertUser registBean=gson.fromJson(jsonObject.toString(),InsertUser.class);
                                if(registBean.getMsg().equals("OK")) {
                                    Toast.makeText(getApplication(), "注册成功", Toast.LENGTH_SHORT).show();
                                    startActivity(new Intent(RegistActivity.this,LoginActivity.class));
                                }
                                else if(registBean.getMsg().equals("F"))
                                    Toast.makeText(getApplication(),registBean.getMsg(),Toast.LENGTH_SHORT).show();
                                else
                                    Toast.makeText(getApplication(),"注册异常",Toast.LENGTH_SHORT).show();

                            }
                        },
                        new Response.ErrorListener() {
                            @Override
                            public void onErrorResponse(VolleyError volleyError) {
                                Toast.makeText(getApplication(),volleyError.toString(),Toast.LENGTH_SHORT).show();
                            }
                        }
                );
                Volley.newRequestQueue(getApplication()).add(jsonObjectRequest);
                break;
        }
    }
    private void submit() {
        // validate
        String usernameString = username.getText().toString().trim();
        if (TextUtils.isEmpty(usernameString)) {
            Toast.makeText(this, "usernameString不能为空", Toast.LENGTH_SHORT).show();

            return;
        }
        String passwordString = password.getText().toString().trim();
        if (TextUtils.isEmpty(passwordString)) {
            Toast.makeText(this, "passwordString不能为空", Toast.LENGTH_SHORT).show();
            return;
        }
    }
}
